package com.youwatch.concurrency.example.singleton;

import com.youwatch.concurrency.annotations.NotRecommend;
import com.youwatch.concurrency.annotations.ThreadSafe;

/**
 * @Author: kss
 * @Description: 单例-懒汉模式-单例实例在第一次使用时进行创建
 * @Date: Create in 2019/1/17
 */
@ThreadSafe
@NotRecommend
public class SingletonLazybonesSafeNotRecommend {

    // 构造方法私有化
    private SingletonLazybonesSafeNotRecommend() {
        // 很多运算操作...初始化多次可能会出错
    }

    // 单例对象
    private static SingletonLazybonesSafeNotRecommend instance = null;

    // 静态工厂方法
    // 通过添加Synchronized关键字来实现线程安全，但是带来了性能上面的开销
    public static synchronized SingletonLazybonesSafeNotRecommend getInstance() {
        if (instance == null) {
            instance = new SingletonLazybonesSafeNotRecommend();
        }
        return instance;
    }

}
